/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.yokeyword.sample.demo_flow.ui.fragment.home;

import me.yokeyword.eventbusactivityscope.EventBusAbilityScope;
import me.yokeyword.sample.ResourceTable;
import me.yokeyword.sample.demo_flow.base.MySupportFraction;
import me.yokeyword.sample.demo_flow.model.UserAccount;
import me.yokeyword.sample.util.ToastDialog;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.TextField;

public class RegisterFraction extends MySupportFraction {
    private TextField mEtAccount,mEtPassword,mEtPasswordConfirm;

    public static RegisterFraction newInstance() {
        RegisterFraction fraction = new RegisterFraction();
        return fraction;
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_layout_fraction_register, container, false);
        mEtAccount = (TextField) component.findComponentById(ResourceTable.Id_et_username);
        mEtPassword = (TextField) component.findComponentById(ResourceTable.Id_et_password);
        mEtPasswordConfirm = (TextField) component.findComponentById(ResourceTable.Id_et_cnf_password);

        component.findComponentById(ResourceTable.Id_register).setClickedListener(component1 -> {
           String strAccount = mEtAccount.getText();
           String strPassword = mEtPassword.getText();
           String strPasswordConfirm = mEtPasswordConfirm.getText();
           if (strAccount.equals("")) {
               ToastDialog.showToast(component1.getContext(),"This username is invalid",500);
               return;
           }

           if (strPassword.trim().equals("") || strPasswordConfirm.trim().equals("")) {
               ToastDialog.showToast(component1.getContext(),"This password is invalid",500);
               return;
           }
           EventBusAbilityScope.getDefault(getFractionAbility()).post(new UserAccount(strAccount));
           popTo(LoginFraction.class, true);
        });
        return component;
    }
}
